package com.zhong.subject.domain.handler.subject;

import com.zhong.subject.common.enums.IsDeleteFlagEnum;
import com.zhong.subject.common.enums.SubjectInfoTypeEnum;
import com.zhong.subject.domain.bo.SubjectAnswerBO;
import com.zhong.subject.domain.bo.SubjectInfoBO;
import com.zhong.subject.domain.bo.SubjectOptionBO;
import com.zhong.subject.domain.convert.SubjectMultipleConverter;
import com.zhong.subject.infra.basic.entity.SubjectMultiple;
import com.zhong.subject.infra.basic.service.SubjectMultipleService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;

/**
 * 多选题目策略类
 */
@Component
public class MultipleTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectMultipleService subjectMultipleService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.MULTIPLE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //多选题目插入
        List<SubjectMultiple> subjectMultipleList = new LinkedList<>();
        subjectInfoBO.getOptionList().forEach(option->{
            SubjectMultiple subjectMultiple = SubjectMultipleConverter.INSTANCE.convertBoToMultiple(option);
            subjectMultiple.setSubjectId(subjectInfoBO.getId());
            subjectMultiple.setIsDelete(IsDeleteFlagEnum.UN_DELETE.getCode());
            subjectMultipleList.add(subjectMultiple);
        });

        subjectMultipleService.batchInsert(subjectMultipleList);
    }

    @Override
    public SubjectOptionBO query(Long subjectId) {
        //多选题目查询
        SubjectMultiple subjectMultiple = new SubjectMultiple();
        subjectMultiple.setSubjectId(subjectId);
        List<SubjectMultiple> subjectMultipleList = subjectMultipleService.queryByCondition(subjectMultiple);
        List<SubjectAnswerBO> subjectAnswerBOList = SubjectMultipleConverter.INSTANCE.convertsMultipleListToAnswerBO(subjectMultipleList);

        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOList);
        return subjectOptionBO;
    }
}
